package org.gitman.filmBase.view.tools;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

public class ResourcesLoader {

	private static ResourcesLoader instance;
	private Map<String, ImageIcon> files2icons;
	private static Logger logger = Logger.getLogger(ResourcesLoader.class);
	
	private ResourcesLoader(){
		files2icons = new HashMap<String, ImageIcon>();
	}
	
	public static ResourcesLoader getInstance(){
		if(instance == null){
			instance = new ResourcesLoader();
		}
		return instance;
	}
	
	public BufferedImage getImage(String imageName) throws IOException {
		return ImageIO.read(new File("data/images/" + imageName));
	}
	
	public ImageIcon getSmallIcon(String iconName) {
		String loc = "data/icons_small/" + iconName;
		ImageIcon icon = files2icons.get(loc);
		if(icon == null){
			icon = new ImageIcon(loc);
			files2icons.put(iconName, icon);
		}
		return icon;
	}
	
	public ImageIcon getBigIcon(String iconName) {
		String loc = "data/icons_big/" + iconName;
		ImageIcon icon = files2icons.get(loc);
		if(icon == null){
			icon = new ImageIcon(loc);
			//icon = new ImageIcon(icon.getImage().getScaledInstance(16,16, 0));
			files2icons.put(iconName, icon);
		}
		return icon;
	}
}
